package com.atjinggu.jinggumail.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atjinggu.jinggumail.product.service.CategoryService;
import com.atjinggu.jinggumail.product.vo.AttrAttrGroupRelationVo;
import com.atjinggu.jinggumail.product.vo.AttrGroupWithAttrsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atjinggu.jinggumail.product.entity.AttrGroupEntity;
import com.atjinggu.jinggumail.product.service.AttrGroupService;
import com.atjinggu.jinggumail.common.utils.PageUtils;
import com.atjinggu.jinggumail.common.utils.R;


/**
 * 属性分组
 *
 * @author jingming
 * @email jingming@gmail.com
 * @date 2020-12-13 00:06:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;


//http://localhost:9001/api/product/attrgroup/1/attr/relation?t=1608624792390
    //http://localhost:9001/api/product/attrgroup/1/attr/relation?t=1608690013240

    //http://localhost:9001/api/product/attrgroup/attr/relation/delete

    //http://localhost:9001/api/product/attrgroup/1/noattr/relation?t=1608692961628&page=1&limit=10&key=

    //http://localhost:9001/api/product/attrgroup/attr/relation


    //http://localhost:9001/api/product/attrgroup/225/withattr?t=1608805555083

    /**
     *
     * @param catelogId
     * @return
     */
    @GetMapping("/{catelogId}/withattr")
    public R withAttr(@PathVariable("catelogId")Long catelogId){
        List<AttrGroupWithAttrsVo> vos =  attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
       return R.ok().put("data",vos);
    };

    /**
     * 批量新增属性和属性分组关联关系
     */
    @PostMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:save")
    public R attrRelation(@RequestBody AttrAttrGroupRelationVo[] relations) {
        attrGroupService.saveAttrRelation(Arrays.asList(relations));
        return R.ok();
    }
    /**
     * 查询未关联当前属性分组的属性的列表
     */
    /**
     * 列表
     */
    @GetMapping("/{attrGroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R listNoAttrRelation(@RequestParam Map<String, Object> params, @PathVariable Integer attrGroupId) {
        PageUtils page = attrGroupService.listNoAttrRelation(params, attrGroupId);

        return R.ok().put("data", page);
    }


    /**
     * 删除属性与属性分组关联关系
     */
    @DeleteMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R attrRelationDelete(@RequestBody AttrAttrGroupRelationVo[] relations) {
        attrGroupService.attrRelationDelete(Arrays.asList(relations));
        return R.ok();
    }


    /**
     * 查询某个属性分组下的所有属性信息
     */
    /**
     * 列表
     */
    @GetMapping("/{attrGroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R listAttrRelation(@RequestParam Map<String, Object> params, @PathVariable Integer attrGroupId) {
        PageUtils page = attrGroupService.listAttrRelation(params, attrGroupId);

        return R.ok().put("data", page);
    }


    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable Integer catId) {
        PageUtils page = attrGroupService.queryPage(params, catId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogIds(categoryService.getPaths(attrGroup.getCatelogId()));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
